Skip to content

Method: TicTacToePosition(int, int)

1: /*
2: * Copyright © 2021 Fachhochschule für die Wirtschaft (FHDW) Hannover
3: *
4: * This file is part of ipspiel22-tictactoe-core.
5: *
6: * ipspiel22-tictactoe-core is free software: you can redistribute it and/or modify it under
7: * the terms of the GNU General Public License as published by the Free Software
8: * Foundation, either version 3 of the License, or (at your option) any later
9: * version.
10: *
11: * ipspiel22-tictactoe-core is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14: * details.
15: *
16: * You should have received a copy of the GNU General Public License along with
17: * ipspiel22-tictactoe-core. If not, see <http://www.gnu.org/licenses/>.
18: */
19: package de.fhdw.gaming.ipspiel22.tictactoe.core.domain;
20:
21: /**
22: * Represents a field position on a Tic Tac Toe board. It contains a row number and a column number (both zero-based).
23: */
24: public final class TicTacToePosition {
25:
26: /**
27: * The row index.
28: */
29: private final int row;
30: /**
31: * The column index.
32: */
33: private final int column;
34:
35: /**
36: * Creates a position on a Tic Tac Toe board.
37: *
38: * @param row The row number (zero based).
39: * @param column The column number (zero-based).
40: */
41: private TicTacToePosition(final int row, final int column) {
42: this.row = row;
43: this.column = column;
44: }
45:
46: /**
47: * Creates a position on a Tic Tac Toe board.
48: *
49: * @param row The row number (zero based).
50: * @param column The column number (zero-based).
51: * @return The position.
52: */
53: public static TicTacToePosition of(final int row, final int column) {
54: return new TicTacToePosition(row, column);
55: }
56:
57: /**
58: * Returns the zero-based row number.
59: */
60: public int getRow() {
61: return this.row;
62: }
63:
64: /**
65: * Returns the zero-based column number.
66: */
67: public int getColumn() {
68: return this.column;
69: }
70:
71: /**
72: * Creates a position relative to this one. The position is not checked against any bounds.
73: *
74: * @param rowOffset The row offset. May be negative, positive, or zero.
75: * @param columnOffset The column offset. May be negative, positive, or zero.
76: * @return The resulting position.
77: */
78: public TicTacToePosition offset(final int rowOffset, final int columnOffset) {
79: return TicTacToePosition.of(this.row + rowOffset, this.column + columnOffset);
80: }
81:
82: @Override
83: public String toString() {
84: return String.format("%c%d", (char) (this.column + 'A'), this.row + 1);
85: }
86:
87: @Override
88: public int hashCode() {
89: return this.row ^ this.column;
90: }
91:
92: @Override
93: public boolean equals(final Object obj) {
94: if (obj instanceof TicTacToePosition) {
95: final TicTacToePosition other = (TicTacToePosition) obj;
96: return this.column == other.column && this.row == other.row;
97: }
98: return false;
99: }
100: }